package ylesanded.hangman;

import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

/**
 * @author joosep.pollumae
 * Logic for creating hidden guessword and validating user guess entry
 */
public class InputHandler {
	

	/**
	 * Constant EXIT.
	 */
	public final int EXIT = 0;
	/**
	 * Constant UNDO.
	 */
	public final int UNDO = 1;
	/**
	 * Constant NOT_VALID.
	 */
	public final int NOT_VALID = 2;
	/**
	 * Constant VALID.
	 */
	public final int VALID = 3;
	/**
	 * Constant NOT_ALLOWED.
	 */
	public final int NOT_ALLOWED = 4;
	
	/**
	 * Holding inserted correct characters.
	 */
	public Set<String> correctChars = new HashSet<String>();
	

	
	/**
	 * @param input inserted by user
	 * @return constant for UI
	 */
	public int validateInput(String input) {
		
		
		
		
		int inputLength = input.length();
		int validated = VALID;

		if (input.equalsIgnoreCase("exit")) {
			
			validated = EXIT;
			
		} else if (input.equalsIgnoreCase("undo")) {
			
			validated = UNDO;
			
		} else if (inputLength > 1) {
			
			validated = NOT_VALID;
			
		} else if (!StringUtils.isAlpha(input)) {
			
			validated = NOT_ALLOWED;
		}
		
		return validated;	
	}
	
	/**
	 * @param guessWord HangString type Strings
	 * @return hidden guessword
	 * Builds guessword with underscores and hyphens
	 */
	public String guessWordBuilder(HangString guessWord) {

		
		StringBuilder hiddenGuessWord = new StringBuilder();
		
	    for (int i = 0; i < guessWord.getStr().length(); ++i) {
	    	
	      Character localCharacter 
	      = Character.valueOf(guessWord.getStr().charAt(i)).charValue();
	      
	      if (localCharacter  == ' ' || localCharacter.charValue() == '-') {
	    	  
	        hiddenGuessWord.append("-");
	     
	      } else if (correctChars.contains(localCharacter.charValue() + "")) {
	        
	    	  hiddenGuessWord.append(localCharacter + " "); 
	    	  
	      } else {
	    	  
	    	  hiddenGuessWord.append("_ ");
	    	  
	      }
	      
	    }
	    
	    return hiddenGuessWord.toString().toUpperCase();	
		
	}
}
